package com.music.mapper;

import com.music.pojo.UserInfo;
import com.music.pojo.UserInfoExample;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface UserInfoMapper {
    @SelectProvider(type=UserInfoSqlProvider.class, method="countByExample")
    long countByExample(UserInfoExample example);

    @DeleteProvider(type=UserInfoSqlProvider.class, method="deleteByExample")
    int deleteByExample(UserInfoExample example);

    @Delete({
        "delete from user",
        "where uid = #{uid,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer uid);

    @Insert({
        "insert into user (uid, username, ",
        "password, birthday, ",
        "email, sex, status)",
        "values (#{uid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, ",
        "#{password,jdbcType=VARCHAR}, #{birthday,jdbcType=DATE}, ",
        "#{email,jdbcType=VARCHAR}, #{sex,jdbcType=CHAR}, #{status,jdbcType=INTEGER})"
    })
    int insert(UserInfo row);

    @InsertProvider(type=UserInfoSqlProvider.class, method="insertSelective")
    int insertSelective(UserInfo row);

    @SelectProvider(type=UserInfoSqlProvider.class, method="selectByExample")
    @Results({
        @Result(column="uid", property="uid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="username", property="username", jdbcType=JdbcType.VARCHAR),
        @Result(column="password", property="password", jdbcType=JdbcType.VARCHAR),
        @Result(column="birthday", property="birthday", jdbcType=JdbcType.DATE),
        @Result(column="email", property="email", jdbcType=JdbcType.VARCHAR),
        @Result(column="sex", property="sex", jdbcType=JdbcType.CHAR),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER)
    })
    List<UserInfo> selectByExample(UserInfoExample example);

    @Select({
        "select",
        "uid, username, password, birthday, email, sex, status",
        "from user",
        "where uid = #{uid,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="uid", property="uid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="username", property="username", jdbcType=JdbcType.VARCHAR),
        @Result(column="password", property="password", jdbcType=JdbcType.VARCHAR),
        @Result(column="birthday", property="birthday", jdbcType=JdbcType.DATE),
        @Result(column="email", property="email", jdbcType=JdbcType.VARCHAR),
        @Result(column="sex", property="sex", jdbcType=JdbcType.CHAR),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER)
    })
    UserInfo selectByPrimaryKey(Integer uid);

    @UpdateProvider(type=UserInfoSqlProvider.class, method="updateByExampleSelective")
    int updateByExampleSelective(@Param("row") UserInfo row, @Param("example") UserInfoExample example);

    @UpdateProvider(type=UserInfoSqlProvider.class, method="updateByExample")
    int updateByExample(@Param("row") UserInfo row, @Param("example") UserInfoExample example);

    @UpdateProvider(type=UserInfoSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(UserInfo row);

    @Update({
        "update user",
        "set username = #{username,jdbcType=VARCHAR},",
          "password = #{password,jdbcType=VARCHAR},",
          "birthday = #{birthday,jdbcType=DATE},",
          "email = #{email,jdbcType=VARCHAR},",
          "sex = #{sex,jdbcType=CHAR},",
          "status = #{status,jdbcType=INTEGER}",
        "where uid = #{uid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(UserInfo row);

    @Select("select * from user where uid = #{uid};")
    UserInfo findByUid(int uid);

    @Select("select * from user where uid in (select uid from user_has_playlist where plid = #{plid})")
    List<UserInfo> findByPlid(int plid);


    @Select("select * from user where uid in (select uid from user_follow_singer where sid = #{sid})")
    List<UserInfo> findBySid(int sid);

    @Select("select * from user where uid = #{uid}")
    @Results(id = "upscaMap",value = {
            @Result(id = true,column = "uid",property = "uid"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "birthday",property = "birthday"),
            @Result(column = "email",property = "email"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "status",property = "status"),
            @Result(column = "uid",property = "singers",
            many = @Many(select = "com.music.mapper.SingerMapper.findByUid")),
            @Result(column = "uid",property = "playlists",
            many = @Many(select = "com.music.mapper.PlaylistMapper.findWithSongsByUid"))
    })
    UserInfo findWithPlayListAndSingerAndCommentsAndActivities(int uid);


    @Insert("insert user_follow_singer(uid,sid) values (#{uid},#{sid})")
    void addUserSinger(@Param("sid") Integer sid,@Param("uid") Integer uid);

    @Delete("delete from user_follow_singer where uid = #{uid} and sid = #{sid}")
    void removeUserSinger(@Param("sid") Integer sid,@Param("uid") Integer uid);


    @Select("select * from user where uid = #{uid}")
    @Results(value = {
            @Result(id = true,column = "uid",property = "uid"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "birthday",property = "birthday"),
            @Result(column = "email",property = "email"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "status",property = "status"),
            @Result(column = "uid",property = "singers",
                    many = @Many(select = "com.music.mapper.SingerMapper.findByUid")),

    })
    UserInfo findUserDetailByUid(Integer uid);
}